﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BomComparison.Core.Model;

public class BomHeaderMap
{
    private int _partNumberCol;
    private int _quantityCol;
    private int _descriptionCol;
    private int _referenceDesignatorCol;
    private int _titleRow;

    public int PartNumberCol { get => _partNumberCol; set => _partNumberCol = value; }
    public int QuantityCol { get => _quantityCol; set => _quantityCol = value; }
    public int DescriptionCol { get => _descriptionCol; set => _descriptionCol = value; }
    public int ReferenceDesignatorCol { get => _referenceDesignatorCol; set => _referenceDesignatorCol = value; }
    public int TitleRow { get => _titleRow; set => _titleRow = value; }

    public BomHeaderMap()
    {
    }

    #region 将用字母表示的Excel列转化为用数字表示
    public static bool IsValidAddress(string address)
    {
        if (string.IsNullOrEmpty(address))
        {
            return false;
        }
        return address.All(IsCol);
    }

    private static bool IsCol(char c)
    {
        return c >= 'A' && c <= 'Z';
    }

    private static int GetColumn(string sCol)
    {
        if (string.IsNullOrEmpty(sCol.Trim()))
        {
            throw new ArgumentException("Column address cannot be empty or null.");
        }

        sCol = sCol.ToUpper();
        var col = 0; // 初始化列号为0
        var len = sCol.Length - 1; // 获取输入字符串的长度减1
        for (var i = len; i >= 0; i--) // 从最后一个字符开始遍历字符串
        {
            // 将当前字符转换为ASCII码值，减去64得到对应数字
            var num = sCol[i] - 64;
            // 计算当前字符对应的数字乘以26的相应次方
            var power = Math.Pow(26, len - i);
            // 累加计算结果到列号中
            col += num * (int)power;
        }
        return col; // 返回最终的列号
    }

    #endregion
}
